** Data preparing for partner change analysis 

* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
/* For Table 3: B Placebo checks */
* - ${output_data}mexico_for_analysis_07_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_08_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_09_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_07_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_08_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_09_11_hs6_restrict.dta
*
/* For Table A12 and A13: Regression with controls */
* - ${output_data}control_vars/mainpartnershare_importer2004.dta
* - ${output_data}control_vars/nbuyer2004.dta
* - ${output_data}control_vars/nseller2004.dta
* - ${output_data}control_vars/nexp2004.dta
* - ${output_data}control_vars/nimp2004.dta
* - ${output_data}control_vars/logtrade2004.dta
* - ${output_data}control_vars/logfirmtrade2004.dta
* - ${output_data}control_vars/logfirmexport2004.dta
* - ${output_data}control_vars/exporter_nproduct.dta
* - ${output_data}control_vars/importer_nproduct.dta
*
/* For Table A15: Main Partner Changes Beyond and Within Initial Partners */
* - ${output_data}mexico_for_within_across.dta
* - ${output_data}us_for_within_across.dta

*************************
*Step 1 Ranking data for 2007-2011 2008-2011 2009-2011
*************************

*************************
*Step 1.1 Create Mexican downgrading 6-digit level restrict data, 2007-2011 2008-2011 2009-2011
*************************
foreach j in 7 8 9{

* Create data for making rankings in 200`j'
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2 value_exporter_importer
keep if year==200`j'
label var value_exporter_importer "exports of hs6 between the exporter and the importer in year"
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gen hs6_exporter_id=hs6+"_"+ rfc
save "${temp_data}data_for_ranking_hs6.dta", replace

* Identify partner with highest match value
use "${temp_data}data_for_ranking_hs6.dta", clear
gsort hs6 -value_exporter_importer newclid2
by hs6: gen mv_rank=_n
keep hs6_importer_id hs6_exporter_id mv_rank
tostring mv_rank, generate(mv_rank_s)
gen hs6_importer_id_mv_rank= hs6_importer_id+"_"+ mv_rank_s
keep hs6_exporter_id hs6_importer_id_mv_rank
sort hs6_importer_id_mv_rank
rename hs6_exporter_id mv_partner
label var mv_partner "id of exporter with whom importer has largest match value"
save "${temp_data}data_for_ranking_hs6_mv_partner.dta", replace

* Create ranking of match values in 200`j' (base year)
use "${temp_data}data_for_ranking_hs6.dta", clear
gsort hs6 -value_exporter_importer newclid2
by hs6: gen mv_rank=_n
collapse (min) mv_rank, by(hs6_importer_id)
tostring mv_rank, generate(mv_rank_s)
gen hs6_importer_id_mv_rank= hs6_importer_id+"_"+ mv_rank_s
drop mv_rank_s
gen hs6=substr(hs6_importer_id,1,6)
gsort hs6 mv_rank
by hs6: replace mv_rank=_n
label var mv_rank "ranking of match values in hs6 for importer"
drop hs6
sort hs6_importer_id_mv_rank
merge 1:1 hs6_importer_id_mv_rank using "${temp_data}data_for_ranking_hs6_mv_partner.dta"
keep if _merge==3
drop _merge  hs6_importer_id_mv_rank
sort hs6_importer_id
save "${temp_data}importer_ranking_hs6.dta", replace

* Create data on export values between importers and exporters
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict", clear
keep rfc hs6 year newclid2 value_exporter_importer
collapse (sum) value_exporter_importer, by(rfc newclid2 year hs6)
gen hs6_exporter_id= hs6+"_"+ rfc
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
save "${temp_data}exporter_importer_hs6_restrict.dta", replace

* Create data for rankings 
use "${temp_data}exporter_importer_hs6_restrict.dta", clear
tostring year, generate(year_s)
gen hs_exporter_id_year= hs6_exporter_id+"_"+year_s
gsort hs_exporter_id_year -value_exporter_importer
by hs_exporter_id_year: gen importer_rank=_n
save "${temp_data}down_grading_hs6.dta", replace

* Identify main partner in 200`j'
use "${temp_data}down_grading_hs6.dta", clear 
keep if importer_rank==1 
keep if year==200`j'
keep hs6_exporter_id  hs6_importer_id
sort hs6_importer_id
merge m:1 hs6_importer_id using "${temp_data}importer_ranking_hs6.dta"
drop if _merge!=3
rename hs6_importer_id main200`j'_id
rename mv_rank mv_rank200`j'
keep hs6_exporter_id main200`j'_id mv_rank200`j'
save "${temp_data}main200`j'_hs6.dta", replace

* Identify main partner in 2011
use "${temp_data}down_grading_hs6.dta", clear 
keep if importer_rank==1
keep if year==2011
keep hs6_exporter_id hs6_importer_id
sort hs6_importer_id
merge m:1 hs6_importer_id using "${temp_data}importer_ranking_hs6.dta"
drop if _merge!=3
rename hs6_importer_id main2011_id
rename mv_rank mv_rank2011
keep hs6_exporter_id main2011_id mv_rank2011
save "${temp_data}main2011_hs6.dta", replace

* Create data on 2007-2011, 2008-2011, and 2009-2011 for Mexico (for conducting placebo checks)
* merge of rankings in 200`j' and 2011 
use "${temp_data}down_grading_hs6.dta", clear 
keep if year==200`j'
keep hs6 rfc  hs6_exporter_id
duplicates drop
sort hs6_exporter_id
merge m:1 hs6_exporter_id using "${temp_data}main200`j'_hs6.dta"
drop if _merge==2
drop _merge
sort hs6_exporter_id
merge m:1 hs6_exporter_id using "${temp_data}main2011_hs6.dta"
drop if _merge==2
drop _merge
sort hs6

* Identify whether the ranking was downgrading
gen mv_rank_d110`j'= mv_rank2011 -mv_rank200`j'
label var mv_rank_d110`j' "mv_rank_2011-mv_rank_200`j'"
gen i_mv_rank_d110`j'=1 if mv_rank_d110`j'>0
replace  i_mv_rank_d110`j'=0 if mv_rank_d110`j'<=0
replace  i_mv_rank_d110`j'=. if mv_rank_d110`j'==.

* Create data for analysis (Mexico)
keep hs6 main200`j'_id hs6_exporter_id mv_rank_d110`j' mv_rank200`j' mv_rank2011 i_mv_rank_d110`j'
gen newclid2=substr(main200`j'_id, 8, length(main200`j'_id))
destring newclid2, replace
sort hs6
* Merge with quota data (for making a binding indicator)
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge==2
drop _merge
gen hs2=substr(hs6,1,2)
save "${output_data}mexico_for_analysis_0`j'_11_hs6_restrict.dta", replace
}

*************************
*Step 1.2 Create US upgrading 6-digit level restrict data, 2007-2011 2008-2011 2009-2011
*************************
foreach j in 7 8 9{

use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2 value_exporter_importer
keep if year==200`j'
label var value_exporter_importer "exports of hs6 between the exporter and the importer in year"
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gen hs6_exporter_id=hs6+"_"+ rfc
save "${temp_data}data_for_ranking_hs6.dta", replace

* Identify partner with highest match value
use "${temp_data}data_for_ranking_hs6.dta", replace
gsort hs6 -value_exporter_importer rfc 
by hs6: gen e_mv_rank=_n
keep hs6_importer_id hs6_exporter_id e_mv_rank 
tostring e_mv_rank, generate(e_mv_rank_s)
gen hs6_exporter_id_e_mv_rank= hs6_exporter_id+"_"+ e_mv_rank_s
keep hs6_exporter_id hs6_exporter_id_e_mv_rank
sort hs6_exporter_id_e_mv_rank		
rename hs6_exporter_id e_mv_partner
label var e_mv_partner "id of exporter's main importer"
save "${temp_data}data_for_ranking_hs6_mv_partner_exp.dta", replace
	
* Create ranking of match values in 200`j' (base year)
use "${temp_data}data_for_ranking_hs6.dta", clear
gsort hs6 -value_exporter_importer rfc
by hs6: gen e_mv_rank=_n
collapse (min) e_mv_rank, by(hs6_exporter_id)
tostring e_mv_rank, generate(e_mv_rank_s)
gen hs6_exporter_id_e_mv_rank= hs6_exporter_id+"_"+ e_mv_rank_s
drop e_mv_rank_s
gen hs6=substr(hs6_exporter_id,1,6)
gsort hs6 e_mv_rank
by hs6: replace e_mv_rank=_n
label var e_mv_rank "ranking of match values in hs6 for exporter"
drop hs6
sort hs6_exporter_id_e_mv_rank
merge 1:1 hs6_exporter_id_e_mv_rank using "${temp_data}data_for_ranking_hs6_mv_partner_exp.dta"
keep if _merge==3
drop _merge  hs6_exporter_id_e_mv_rank
sort hs6_exporter_id
save "${temp_data}exporter_ranking_hs6.dta", replace

* Create data for rankings 
use "${temp_data}exporter_importer_hs6_restrict.dta", clear
tostring year, generate(year_s)
gen hs_importer_id_year= hs6_importer_id+"_"+year_s
gsort hs_importer_id_year -value_exporter_importer
by hs_importer_id_year: gen exporter_rank=_n
save "${temp_data}up_grading_hs6.dta", replace

* Identify main partner in 200`j'
use "${temp_data}up_grading_hs6.dta", clear 
keep if exporter_rank==1 
keep if year==200`j'
keep hs6_exporter_id  hs6_importer_id
sort hs6_exporter_id
merge m:1 hs6_exporter_id using "${temp_data}exporter_ranking_hs6.dta"
drop if _merge!=3
rename hs6_exporter_id main200`j'_id
rename e_mv_rank e_mv_rank200`j'
keep hs6_importer_id main200`j'_id e_mv_rank200`j'
save "${temp_data}main200`j'_exp_hs6.dta", replace

* Identify main partner in 2011
use "${temp_data}up_grading_hs6.dta", clear
keep if exporter_rank==1
keep if year==2011
keep hs6_exporter_id  hs6_importer_id
sort hs6_exporter_id
merge m:1 hs6_exporter_id using "${temp_data}exporter_ranking_hs6.dta"
drop if _merge!=3
rename hs6_exporter_id main2011_id
rename e_mv_rank e_mv_rank2011
keep hs6_importer_id main2011_id e_mv_rank2011
save "${temp_data}main2011_exp_hs6.dta", replace

* Create data on 2007-2011, 2008-2011, and 2009-2011 for US (for conducting placebo checks)
* merge of rankings in 200`j' and 2011 
use "${temp_data}up_grading_hs6.dta", clear 
keep if year==200`j'
keep hs6 importer_id  hs6_importer_id 
duplicates drop
merge m:1 hs6_importer_id using "${temp_data}main200`j'_exp_hs6.dta"
drop if _merge==2
drop _merge
merge m:1 hs6_importer_id using "${temp_data}main2011_exp_hs6.dta"
drop if _merge==2
drop _merge
sort hs6

* Identify whether the ranking was upgrading
gen e_mv_rank_d110`j'= e_mv_rank2011 -e_mv_rank200`j'
gen i_e_mv_rank_d110`j'=1 if e_mv_rank_d110`j'<0
replace  i_e_mv_rank_d110`j'=0 if e_mv_rank_d110`j'>=0
replace  i_e_mv_rank_d110`j'=. if e_mv_rank_d110`j'==.
label var e_mv_rank_d110`j' "e_mv_rank_2011-e_mv_rank_200`j'"
label var i_e_mv_rank_d110`j' "1 if e_mv_rank_d110`j'<0 (upgrade) and 0 otherwise"

* Create data for analysis (US)
keep hs6 main200`j'_id hs6_importer_id e_mv_rank_d110`j' i_e_mv_rank_d110`j' e_mv_rank200`j' e_mv_rank2011 
gen newclid2=substr(hs6_importer_id, 8, length(main200`j'_id))
destring newclid2, replace
* Merge with quota data (for making a binding indicator)
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge==2
drop _merge
gen hs2=substr(hs6,1,2)
save "${output_data}US_for_analysis_0`j'_11_hs6_restrict.dta", replace
}


*************************
*Step 2 Preparing control variables (For Table A12 and A13)
*************************

* Make Maquiladora
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
collapse (sum) value_exporter_importer, by(rfc hs6 year newclid2)
keep if year==2004
collapse (max) maxvalue=value_exporter_importer (sum) sumvalue=value_exporter_importer, by(newclid2 hs6)
gen mainpartnershare_importer=maxvalue/sumvalue
keep newclid2 hs6 mainpartnershare_importer
save "${output_data}control_vars/mainpartnershare_importer2004.dta", replace 

use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
egen value_exp_imp=total(value_exporter_importer), by (rfc newclid2 hs6 year)
gen ratio_maquil=value_exporter_importer/value_exp_imp if maquila==1
replace ratio_maquil=0 if ratio_maquil==.
egen ratio_maquila=max(ratio_maquil), by(rfc newclid2 hs6)
bys rfc newclid2 hs6: gen indicator=_n
keep if indicator==1
keep rfc hs6 newclid2 ratio_maquila
destring hs6, replace
save "${output_data}control_vars/maquila_ratio_hs6_restrict.dta", replace 


use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
collapse Retail Manufacturing Wholesale Other, by(newclid2)

        foreach var of varlist Retail Manufacturing Wholesale Other{
                rename `var' `var'2
        }
		
save "${output_data}control_vars/Naics", replace 


* Make #Partners
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
collapse (sum) value_exporter_importer , by(rfc hs6 year newclid2)
keep if year==2004
bys rfc hs6: gen nbuyer2004=_N
collapse nbuyer2004, by(rfc hs6)
save "${output_data}control_vars/nbuyer2004.dta", replace 

use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
collapse (sum) value_exporter_importer , by(rfc hs6 year newclid2)
keep if year==2004
bys newclid2 hs6: gen nseller2004=_N
collapse nseller2004, by(newclid2 hs6)
save "${output_data}control_vars/nseller2004.dta", replace 

* Make #Exporters
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
drop if newclid2==.
gen nexp2004=1
collapse nexp2004, by (rfc hs6)
collapse (sum) nexp2004, by(hs6)
save "${output_data}control_vars/nexp2004.dta", replace

* Make #Importers
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
drop if newclid2==.
gen nimp2004=1
collapse nimp2004, by (newclid2 hs6)
collapse (sum) nimp2004, by(hs6)
save "${output_data}control_vars/nimp2004.dta", replace

* Make LnTrade
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(hs6)
gen lvalue2004=log(value)
keep hs6 lvalue2004
save "${output_data}control_vars/logtrade2004.dta", replace

* Make LnTotalTrade (Import)
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(newclid2 hs6)
gen limportvalue2004=log(value)
keep newclid2 hs6 limportvalue2004
save "${output_data}control_vars/logfirmtrade2004.dta", replace

* Make LnTotalTrade (Export)
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc hs6)
gen lexportvalue2004=log(value)
keep rfc hs6 lexportvalue2004
save "${output_data}control_vars/logfirmexport2004.dta", replace

* Make N of products
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2 value_exporter_importer
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc hs6)
bys rfc: gen nproduct=_N
drop value_exporter_importer hs6
duplicates drop
save "${output_data}control_vars/exporter_nproduct.dta", replace

use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2 value_exporter_importer
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(newclid2 hs6)
bys newclid2: gen nproduct=_N
drop value_exporter_importer hs6
duplicates drop
save "${output_data}control_vars/importer_nproduct.dta", replace


*************************
*Step 3 Partner change beyond and within initial partners
*************************

** Main partner change beyond and within initial partners
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
keep rfc newclid2 hs6
save "${temp_data}exp_imp_prod_2004.dta", replace

*************************
*Step 3.1 Mexican exporters
*************************
** Define across
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2007
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
gsort hs6 rfc -value_exporter_importer
by hs6 rfc: gen importer_rank=_n
keep if importer_rank==1
keep rfc newclid2 hs6 
save "${temp_data}main_importer_2007.dta", replace

use "${temp_data}main_importer_2007.dta", clear
merge 1:1 rfc newclid2 hs6 using "${temp_data}exp_imp_prod_2004.dta"
drop if _merge!=1
gen across=1
collapse (mean) across, by(rfc hs6)
gen hs6_exporter_id=hs6+"_"+ rfc
save "${temp_data}across.dta", replace

** Define downgrading_within/across
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
keep if i_mv_rank_d0704!=.
collapse (mean) i_mv_rank_d0704, by(hs6_exporter_id)
merge 1:1 hs6_exporter_id using "${temp_data}across.dta"
drop if _merge==2
replace across=1 if _merge==3
gen within=1 if _merge==1
replace across=0 if within==1
replace within=0 if across==1
drop _merge
gen down_within=i_mv_rank_d0704*within
gen down_across=i_mv_rank_d0704*across
drop rfc
drop hs6
gen hs6=substr(hs6_exporter_id,1,6)
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge!=3
drop _merge
drop hs6
rename iq_mex_bind80 Binding
gen hs2=substr(hs6,1,2)
gen product_type="textile" if hs2=="50"|hs2=="51" |hs2=="52" | hs2=="53" | hs2=="54" | hs2=="55"
replace product_type="textile_products" if hs2=="56" | hs2=="57" | hs2=="58" |hs2=="59" | hs2=="60" | hs2=="63"
replace product_type="apparel" if hs2=="61" |hs2=="62"
rename product_type Product
save "${temp_data}down_whithin_across_mex.dta", replace

** Define upgrading_within/across
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
keep if i_up_mv_rank_d0704!=.
collapse (mean) i_up_mv_rank_d0704, by(hs6_exporter_id)
merge 1:1 hs6_exporter_id using "${temp_data}across.dta"
drop if _merge==2
replace across=1 if _merge==3
gen within=1 if _merge==1
replace across=0 if within==1
replace within=0 if across==1
drop _merge
gen up_within=i_up_mv_rank_d0704*within
gen up_across=i_up_mv_rank_d0704*across
drop rfc
drop hs6
gen hs6_str=substr(hs6_exporter_id,1,6)
save "${temp_data}up_whithin_across_mex.dta", replace

use "${temp_data}up_whithin_across_mex.dta", clear
merge 1:1 hs6_exporter_id using "${temp_data}down_whithin_across_mex.dta"
rename hs6_str hs6 
save "${output_data}mexico_for_within_across.dta", replace

*************************
*Step 3.2 US importers
*************************
** Define across
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2007
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
gsort hs6 newclid2 -value_exporter_importer
by hs6 newclid2: gen exporter_rank=_n
keep if exporter_rank==1
keep rfc newclid2 hs6 
save "${temp_data}main_exporter_2007.dta", replace

use "${temp_data}main_exporter_2007.dta", clear
merge 1:1 rfc newclid2 hs6 using "${temp_data}exp_imp_prod_2004.dta"
drop if _merge!=1
gen across=1
collapse (mean) across, by(newclid2 hs6)
tostring newclid2, replace
gen hs6_importer_id=hs6+"_"+newclid2
destring newclid2, replace
save "${temp_data}across_us.dta", replace

** Define upgrading_within/across
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
keep if i_e_mv_rank_d0704!=.
collapse (mean) i_e_mv_rank_d0704, by(hs6_importer_id)
merge 1:1 hs6_importer_id using "${temp_data}across_us.dta"
drop if _merge==2
replace across=1 if _merge==3
gen within=1 if _merge==1
replace across=0 if within==1
replace within=0 if across==1
drop _merge
gen up_within=i_e_mv_rank_d0704*within
gen up_across=i_e_mv_rank_d0704*across
drop newclid2
drop hs6
gen hs6=substr(hs6_importer_id,1,6)
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge!=3
drop _merge hs6
rename iq_mex_bind80 Binding
gen hs2=substr(hs6,1,2)
gen product_type="textile" if hs2=="50"|hs2=="51" |hs2=="52" | hs2=="53" | hs2=="54" | hs2=="55"
replace product_type="textile_products" if hs2=="56" | hs2=="57" | hs2=="58" |hs2=="59" | hs2=="60" | hs2=="63"
replace product_type="apparel" if hs2=="61" |hs2=="62"
rename product_type Product
save "${temp_data}up_within_across_us.dta", replace

** Define downgrading_within/across
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
keep if i_down_mv_rank_d0704!=.
collapse (mean) i_down_mv_rank_d0704, by(hs6_importer_id)
merge 1:1 hs6_importer_id using "${temp_data}across_us.dta"
drop if _merge==2
replace across=1 if _merge==3
gen within=1 if _merge==1
replace across=0 if within==1
replace within=0 if across==1
drop _merge
gen down_within=i_down_mv_rank_d0704*within
gen down_across=i_down_mv_rank_d0704*across
drop newclid2
drop hs6
gen hs6_str=substr(hs6_importer_id,1,6)
save "${temp_data}down_within_across_us.dta", replace

use "${temp_data}down_within_across_us.dta", clear
merge 1:1 hs6_importer_id using "${temp_data}up_within_across_us.dta"
rename hs6_str hs6
save "${output_data}us_for_within_across.dta", replace
